package com.pansoft.openplanet.util;

import com.efounder.util.AppContext;
import com.pansoft.openplanet.bean.Account;
import com.pansoft.openplanet.blockchain.cypto.ec.EosPrivateKey;
import com.pansoft.openplanet.db.AccountDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pocketEos on 2018/2/3.
 */

public class EosPublicAndPrivateKeyUtils {


    //获取钱包下所有账号activepublickey
    public static List<String> getActivePublicKey() {
        List<String> keyList = new ArrayList<>();
        List<Account> accounts = new AccountDao(AppContext.getInstance()).getAllAccount(EosNetParamUtil.getEosChainEnum());//遍历本地所有账号信息
        for (int i = 0; i < accounts.size(); i++) {
            keyList.add(accounts.get(i).getPublicKey());
        }
        return keyList;
    }

    //通过公钥获取私钥
    public static String getPrivateKey(String publicKey, String password) {
        String activePrivateKey = null;
        List<Account> accounts = new AccountDao(AppContext.getInstance()).getAllAccount(EosNetParamUtil.getEosChainEnum());//遍历本地所有账号信息
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getPublicKey().equals(publicKey)) {
                activePrivateKey = accounts.get(i).getSecretPrivateKey();
            }
        }
        if (activePrivateKey != null) {
            String key =null;
            try {
                key =  TCAccountManager.decryptByMainPrivKey(activePrivateKey);
            }  catch (Exception e) {
                e.printStackTrace();
            }
            return key;
        } else {
            return null;
        }
    }

    public static EosPrivateKey[] getPrivateKey(int count) {
        EosPrivateKey[] retKeys = new EosPrivateKey[count];
        for (int i = 0; i < count; i++) {
            retKeys[i] = new EosPrivateKey();
        }

        return retKeys;
    }

    public static void main(String[] args) {
        EosPrivateKey mOwnerKey = EosPublicAndPrivateKeyUtils.getPrivateKey(2)[0];
        EosPrivateKey mActiveKey = EosPublicAndPrivateKeyUtils.getPrivateKey(2)[1];
        String mAccount_owner_public_key = mOwnerKey.getPublicKey().toString();
        String mAccount_active_public_key = mActiveKey.getPublicKey().toString();
        String mAccount_active_private_key = mActiveKey.toString();
        String mAccount_owner_private_key = mOwnerKey.toString();
        System.out.println("owner_public_key:"+mAccount_owner_public_key);
        System.out.println("owner_private_key:"+mAccount_owner_private_key);

        System.out.println("active_public_key:"+mAccount_active_public_key);
        System.out.println("active_private_key:"+mAccount_active_private_key);

    }

}
